package com.ld.hpfs.service.impl;

import java.util.ArrayList;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ld.hpfs.dao.MdataMapper;
import com.ld.hpfs.service.MdataService;

@Service
public class MdataServiceImpl implements MdataService {
	private static final Logger logger = LoggerFactory.getLogger(MdataServiceImpl.class);
	@Autowired
	private MdataMapper mdataMapper;
	
	/*
	public ArrayList<HashMap<String,Object>> getMdataType(HashMap<String,Object> inputparam)
    { 	   	
    	int count =mdataMapper.getMdataTypeCodeListCount(inputparam);
		if(count == 0){
			return null;
		}
		ArrayList<HashMap<String,Object>> result =  mdataMapper.getMdataType(inputparam);
		return result;
    }
	*/
	
	public ArrayList<HashMap<String,Object>> getMdataAccount(HashMap<String,Object> inputparam)
    { 	   	
    	int count = mdataMapper.getMdataAccountListCount(inputparam);
		if(count == 0){
			return null;
		}else{
			inputparam.put("pageCount", count);
			return mdataMapper.getMdataAccount(inputparam);
		}
		
    }
	
	public ArrayList<HashMap<String,Object>> getMdataGoodsList(HashMap<String,Object> inputparam)
    { 	   	
    	int count = mdataMapper.getMdataGoodsListCount(inputparam);
		if(count == 0){
			return null;
		}
		inputparam.put("pageCount", count);
		return mdataMapper.getMdataGoodsList(inputparam);
    }
	
	public ArrayList<HashMap<String,Object>> getMdataClientList(HashMap<String,Object> inputparam)
    { 	   	
    	int count = mdataMapper.getMdataClientListCount(inputparam);
		if(count == 0){
			return null;
		}else{
			inputparam.put("pageCount", count);
			return mdataMapper.getMdataClientList(inputparam);
		}
		
    }
	
	public ArrayList<HashMap<String,Object>> getMdataClientAddressList(HashMap<String,Object> inputparam)
    { 	   	
    	int count = mdataMapper.getMdataClientAddressListCount(inputparam);
		if(count == 0){
			return null;
		}
		inputparam.put("pageCount", count);
		return mdataMapper.getMdataClientAddressList(inputparam);
    }
	
    public int insertMdataClientMappingForImport(HashMap<String,Object> param){
    	try{
//    		if(mdataMapper.checkMdataClientForImport(param) > 0){
//    			return -10;
//    		}
    		mdataMapper.insertMdataClientForImport(param);
    		mdataMapper.updateMdataClientForImport(param);
			return 0;
		}catch(Exception ex){
			ex.printStackTrace();
			logger.error(ex.getMessage());
			return -1;
		}
    }
    
    public int insertMdataClientAddressMappingForImport(HashMap<String,Object> param){
//    		try{
//    			mdataMapper.insertMdataClientAddressForImport(param);
//    		}catch(Exception ex2){
//    			try{
//    				mdataMapper.updateMdataClientAddressForImport(param);
//    			}catch(Exception ex){
//    				ex.printStackTrace();
//    				logger.error(ex.getMessage());
//    				return -1;
//    			}
//    		}
//			return 0;
    	try{
    		mdataMapper.insertMdataClientAddressForImport(param);
    		mdataMapper.updateMdataClientAddressForImport(param);
			return 0;
		}catch(Exception ex){
			ex.printStackTrace();
			logger.error(ex.getMessage());
			return -1;
		}
    }
    
    public int insertMdataGoodsMappingForImport(HashMap<String,Object> param){
    	try{
//    		if(mdataMapper.checkMdataGoodsForImport(param) > 0){
//    			return -10;
//    		}
    		mdataMapper.insertMdataGoodsForImport(param);
    		mdataMapper.updateMdataGoodsForImport(param);
			return 0;
		}catch(Exception ex){
			ex.printStackTrace();
			logger.error(ex.getMessage());
			return -1;
		}
    }
    
    /*
    public int insertMdataTypeMappingForImport(HashMap<String,Object> param){
    	try{
    		mdataMapper.insertMdataTypeForImport(param);
    		mdataMapper.updateMdataTypeForImport(param);
			return 0;
		}catch(Exception ex){
			ex.printStackTrace();
			logger.error(ex.getMessage());
			return -1;
		}
    }
    */
    
    public int insertMdataAccountMappingForImport(HashMap<String,Object> param){
    	try{
//    		if(mdataMapper.checkMdataAccountForImport(param) > 0){
//    			return -10;
//    		}
    		mdataMapper.insertMdataAccountForImport(param);
    		mdataMapper.updateMdataAccountForImport(param);
		}catch(Exception ex){
			ex.printStackTrace();
			logger.error(ex.getMessage());
			return -1;
		}
    	return 0;
    }
	
	public ArrayList<HashMap<String,Object>> getMdataClientMappingForImport(HashMap<String,Object> param){
		return mdataMapper.getMdataClientMappingForImport(param);
	}
	
	public ArrayList<HashMap<String,Object>> getMdataClientAddressMappingForImport(HashMap<String,Object> param){
		return mdataMapper.getMdataClientAddressMappingForImport(param);
	}
	
	public ArrayList<HashMap<String,Object>> getMdataGoodsMappingForImport(int id){
		return mdataMapper.getMdataGoodsMappingForImport(id);
	}
	
	/*
	public ArrayList<HashMap<String,Object>> getMdataTypeMappingForImport(int id){
		return mdataMapper.getMdataGoodsMappingForImport(id);
	}
	*/
	
	public ArrayList<HashMap<String,Object>> getMdataAccountMappingForImport(int id){
		return mdataMapper.getMdataAccountMappingForImport(id);
	}
	
	/*
	public int getMdataTypeCodeFromMdataType(HashMap<String,Object> param){
		HashMap<String,Object> MdataTypeCode = mdataMapper.getMdataTypeCodeFromMdataType(param);
    	if (MdataTypeCode != null)
    	{
    		return Integer.parseInt(MdataTypeCode.get("id").toString());
    	}
    	return -1;
	}
	
	public String addMdataTypeCode(HashMap<String,Object> param){
		try{
			mdataMapper.addMdataTypeCode(param);
		}catch (Exception ex){
			logger.error(ex.getMessage());
			return "-1";
		}
		return "0";
	}
	
	public String updateMdataTypeCode(HashMap<String,Object> param){
		try{
			mdataMapper.updateMdataTypeCode(param);
		}catch (Exception ex){
			logger.error(ex.getMessage());
			return "-1";
		}
		return "0";
	}
	
	public String deleteMdataTypeCode(HashMap<String,Object> param){
		try{
			mdataMapper.deleteMdataTypeCode(param);
		}catch (Exception ex){
			logger.error(ex.getMessage());
			return "-1";
		}
		return "0";
	}
	*/
	
	public int getMdataAccountCodeFromMdataAccount(HashMap<String,Object> param){
		HashMap<String,Object> MdataTypeCode = mdataMapper.getMdataAccountCodeFromMdataAccount(param);
    	if (MdataTypeCode != null)
    	{
    		return Integer.parseInt(MdataTypeCode.get("id").toString());
    	}
    	return -1;
	}
	
	public String addMdataAccountCode(HashMap<String,Object> param){
		try{
			mdataMapper.addMdataAccountCode(param);
		}catch (Exception ex){
			logger.error(ex.getMessage());
			return "-1";
		}
		return "0";
	}
	
	public String updateMdataAccountCode(HashMap<String,Object> param){
		try{
			mdataMapper.updateMdataAccountCode(param);
		}catch (Exception ex){
			logger.error(ex.getMessage());
			return "-1";
		}
		return "0";
	}
	
	public String deleteMdataAccountCode(HashMap<String,Object> param){
		try{
			mdataMapper.deleteMdataAccountCode(param);
		}catch (Exception ex){
			logger.error(ex.getMessage());
			return "-1";
		}
		return "0";
	}
	
	public int getMdataGoodsCodeFromMdataGoods(HashMap<String,Object> param){
		HashMap<String,Object> MdataGoodsCode = mdataMapper.getMdataGoodsCodeFromMdataGoods(param);
    	if (MdataGoodsCode != null)
    	{
    		return Integer.parseInt(MdataGoodsCode.get("id").toString());
    	}
    	return -1;
	}
	
	public String addMdataGoodsCode(HashMap<String,Object> param){
		try{
			mdataMapper.addMdataGoodsCode(param);
		}catch (Exception ex){
			logger.error(ex.getMessage());
			return "-1";
		}
		return "0";
	}
	
	public String updateMdataGoodsCode(HashMap<String,Object> param){
		try{
			mdataMapper.updateMdataGoodsCode(param);
		}catch (Exception ex){
			logger.error(ex.getMessage());
			return "-1";
		}
		return "0";
	}
	
	public String deleteMdataGoodsCode(HashMap<String,Object> param){
		try{
			mdataMapper.deleteMdataGoodsCode(param);
		}catch (Exception ex){
			logger.error(ex.getMessage());
			return "-1";
		}
		return "0";
	}
	
	public int getMdataClientCodeFromMdataClient(HashMap<String,Object> param){
		HashMap<String,Object> MdataClientCode = mdataMapper.getMdataClientCodeFromMdataClient(param);
    	if (MdataClientCode != null)
    	{
    		return Integer.parseInt(MdataClientCode.get("id").toString());
    	}
    	return -1;
	}
	
	public String addMdataClientCode(HashMap<String,Object> param){
		try{
			mdataMapper.addMdataClientCode(param);
		}catch (Exception ex){
			logger.error(ex.getMessage());
			return "-1";
		}
		return "0";
	}
	
	public String updateMdataClientCode(HashMap<String,Object> param){
		try{
			mdataMapper.updateMdataClientCode(param);
		}catch (Exception ex){
			logger.error(ex.getMessage());
			return "-1";
		}
		return "0";
	}
	
	public String deleteMdataClientCode(HashMap<String,Object> param){
		try{
			mdataMapper.deleteMdataClientCode(param);
			mdataMapper.deleteMdataClientWithAddressCode(param);
		}catch (Exception ex){
			logger.error(ex.getMessage());
			return "-1";
		}
		return "0";
	}
	
	public String getMdataClientAddressCodeFromMdataClient(HashMap<String,Object> param){
		HashMap<String,Object> MdataClientCode = mdataMapper.getMdataClientAddressCodeFromMdataClient(param);
    	if (MdataClientCode != null)
    	{
//    		return Integer.parseInt(MdataClientCode.get("id").toString());
    		return MdataClientCode.get("id").toString();
    	}
    	return "NODATA";
	}
	
	public String addMdataClientAddressCode(HashMap<String,Object> param){
		try{
			mdataMapper.addMdataClientAddressCode(param);
		}catch (Exception ex){
			logger.error(ex.getMessage());
			return "-1";
		}
		return "0";
	}
	
	public String updateMdataClientAddressCode(HashMap<String,Object> param){
		try{
			mdataMapper.updateMdataClientAddressCode(param);
		}catch (Exception ex){
			logger.error(ex.getMessage());
			return "-1";
		}
		return "0";
	}
	
	public String deleteMdataClientAddressCode(HashMap<String,Object> param){
		try{
			mdataMapper.deleteMdataClientAddressCode(param);
		}catch (Exception ex){
			logger.error(ex.getMessage());
			return "-1";
		}
		return "0";
	}
	
	@Override
	public ArrayList<HashMap<String, String>> getMdataAcc(HashMap<String, Object> param) {
		return mdataMapper.getMdataAcc(param);
	}
	
}
